﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Framework.Common.Attributes;
using System.Collections;
using System.Globalization;

namespace Framework.Common
{

    public static class EnumHelper
    {
        #region GetDescription
        /// <summary>
        /// 
        /// </summary>
        static Type enumDesciptionType = typeof(Common.Attributes.EnumDescriptionAttribute);
        /// <summary>
        /// 获取描述信息
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            string description = value.ToString();
            FieldInfo fieldInfo = value.GetType().GetField(description);
            var attribute = (EnumDescriptionAttribute)fieldInfo.GetCustomAttributes(enumDesciptionType, true).FirstOrDefault();
            if (attribute != null)
            {
                description = attribute.Description;
            }
            return description;
        }
        #endregion

        #region 获取枚举值
        /// <summary>
        /// 获取枚举值
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static TEnum GetEnum<TEnum>(string input) where TEnum : struct
        {
            TEnum te;
            if (Enum.TryParse<TEnum>(input, true, out te))
            {
                return te;
            }
            return default(TEnum);
        }
        #endregion
    }

    /// <summary>
    /// 枚举描述值字典
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TEnum"></typeparam>
    public static class EnumDict<TKey, TEnum>
    {

        public static Dictionary<TKey, string> Dictionary //= new Dictionary<TKey, string>();
        {
            get
            {
                if (dict == null)
                {
                    lock (locker)
                    {
                        if (dict == null)
                        {
                            InitEnumToDescDict();
                        }
                    }
                }
                return dict;
            }
        }

        private static Dictionary<TKey, string> dict;
        private static object locker = new object();
        #region EnumToDescDict and Key is TKey
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static void InitEnumToDescDict()
        {
            Type type = typeof(TEnum);

            if (!type.IsEnum)
            {
                return;
            }

            Array enumValues = Enum.GetValues(type);
            dict = new Dictionary<TKey, string>();
            foreach (Enum value in enumValues)
            {
                dict.Add((TKey)Convert.ChangeType(value, typeof(TKey), CultureInfo.InvariantCulture)
                    , EnumHelper.GetDescription(value));
            }
        }
        #endregion
    }
}
